const that = this
const gis = this.getApiWidgetByAliasName('三维组件').widgetObj;
const list = this.getApiWidgetsByClassName('event-list');
const event_count = this.getApiWidgetByAliasName('event-count');
const data = this.getVariables().find(v => v.name === "screen_params");

if (data?.value) {
  const params = JSON.parse(data.value);
  console.log('============>>>>>>', params)
  params.eventPoint = `${params.xCoordinate}，${params.yCoordinate}，${params.zCoordinate}`;

  gis.gisMap.setEvent(params);

  gis.setIconPosition({ right: 420, top: 100 });
  gis.setModalPosition({ right: 815, bottom: 20 });
  const { eventDetails } = params;
  const detail = this.getApiWidgetsByClassName('detail');
  const wujing = this.getApiWidgetByAliasName('误警事件');
  const wujingIco = this.getApiWidgetByAliasName('误警图标');
  const jiechu = this.getApiWidgetByAliasName('解除事件');
  const jiechuIco = this.getApiWidgetByAliasName('解除图标');
  const yanpan = this.getApiWidgetByAliasName('detail-judge');
  const yanpanIco = this.getApiWidgetByAliasName('judge-ico');
  const guanbi = this.getApiWidgetByAliasName('关闭');
  const guanbiIco = this.getApiWidgetByAliasName('关闭图标');
  detail.forEach(v => v.showWidget());

  if (params.whetherProcess == '2') {
    $(yanpan.widgetObj.container).find('.label-text')[0].style.color = '#888';
    $(wujing.widgetObj.container).find('.label-text')[0].style.color = '#888';
    yanpan.widgetObj.container.style.pointerEvents = 'none';
    wujing.widgetObj.container.style.pointerEvents = 'none'
  } else if (params.whetherProcess == '0') {
    $(yanpan.widgetObj.container).find('.label-text')[0].style.color = '#FFF';
    $(wujing.widgetObj.container).find('.label-text')[0].style.color = '#FFF';
    $(jiechu.widgetObj.container).find('.label-text')[0].style.color = '#FFF';
    yanpan.widgetObj.container.style.pointerEvents = 'auto';
    wujing.widgetObj.container.style.pointerEvents = 'auto';
    jiechu.widgetObj.container.style.pointerEvents = 'auto'
  } else if (params.whetherProcess == '4' || params.whetherProcess == '1') {
    [ wujing, wujingIco, jiechu, jiechuIco, yanpan, yanpanIco, guanbi, guanbiIco ].forEach(
      widget => widget.hideWidget()
    );
  } else if (params.whetherProcess == '3') {
    $(yanpan.widgetObj.container).find('.label-text')[0].style.color = '#888';
    $(wujing.widgetObj.container).find('.label-text')[0].style.color = '#888';
    $(jiechu.widgetObj.container).find('.label-text')[0].style.color = '#888';
    yanpan.widgetObj.container.style.pointerEvents = 'none';
    wujing.widgetObj.container.style.pointerEvents = 'none';
    jiechu.widgetObj.container.style.pointerEvents = 'none';
  }

  const $form = this.getApiWidgetByAliasName('judge-content');
  const $img = this.getApiWidgetByAliasName('detail-img');
  const levels = this.getDataById('744574925553008640');
  const types = this.getDataById('744575296061046784');
  const qualitys = this.getDataById('756194853578080256');
  // console.log('qualitys', types)
  // 详情时隐藏上传功能
  $form.widgetObj._option.config.forEach(v => {
    if (v.type == 'upload' || v.dataIndex == 'sourceDeviceId') {
      v.visible = true
    }
  });

  $form.widgetObj._option.options = {
    eventTypeCode: types?.data,
    eventGradeCode: levels?.data,
    eventQualityCode: qualitys?.data,
    whetherProcess: [
      { name: '未处理', value: 0 },
      { name: '已处理', value: 1 },
      { name: '处理中', value: 2 },
      { name: '已解除', value: 3 },
      { name: '误警', value: 4 },
    ],
  };
  $form.widgetObj._option.dataSource = params
  $form.widgetObj._option.isDetail = true
  $form.widgetObj.render()
  const form = this.retrievePlugin('base-forms')
  form.resetForm('form_19')
  form.resetForm('form_20')
  form.setFormData('form_19', params)

  const info = JSON.parse(eventDetails)
  if (info.thumbnailPath) {
    const srcArr = info.thumbnailPath.split(',').map(v => ({ src: v }))
    $img.widgetObj.setData([{ data: srcArr }])
    $img.widgetObj.render()
  } else {
    $img.widgetObj.setData([{ data: [] }])
    $img.widgetObj.render()
  }

  // 打开回放
  if (info.jpbPath) {
    const $playback = this.getApiWidgetByAliasName('playback');
    $playback.widgetObj.setData([{ data: info.jpbPath }])
    $playback.showWidget();
    $playback.widgetObj.render();
  } else {
    $playback.hideWidget();
    window.layer.msg('暂无录像回放')
  }

  if (params.sourceDeviceId) {
    // 打开实时视频
    const options = {
      method: 'GET',
      headers: {
        "Content-type": "application/json",
      },
    }
    fetch(`/Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode?equipmentCode=${params.sourceDeviceId}`, options)
      .then(res => res.json())
      .then(json => {
        if (json?.success && json.result) {
          const { rows } = json.result
          const data = rows[0]?.equipmentConfig?.gbVideoConfig
          var host = this.getVariables().find(x => x.name == "stream_url").value;
          gis.sullivan.close()
          if (data && data.sipUser && data.videoChannelId) {
            gis.sullivan.addPlayer(`${host}/api/v1/stream/rtc/play`, `${data.sipUser}/${data.videoChannelId}`)
            gis.sullivan.initial()
          } else {
            window.layer.msg('暂无设备号/通道号')
          }
        }
      })
  } else {
    gis.sullivan.close();
    window.layer.msg('未关联摄像头')
  }
}

list.forEach(v => v.showWidget());

/*
window.onmessage = function (event) {
  console.log('event++++++', event, this)
  const { data } = event
  if (data.name === 'isOther') {
    if (data.value === '0') {
      that.setVariables([{ name: 'whether_process', value: '0' }])
      event_count.widgetObj._option.defaultKey = 'current'
      event_count.widgetObj.render()
    } else {
      that.setVariables([{ name: 'whether_process', value: '2' }])
      event_count.widgetObj._option.defaultKey = 'processing'
      event_count.widgetObj.render()
    }
  }
}*/
